package com.fangcun.service.api.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.fangcun.bean.admin.agent.response.AgentShareBenefitsRuleDetail;
import com.fangcun.bean.api.agent.InvitationRecordResponse;
import com.fangcun.bean.api.agent.InvitationResponse;
import com.fangcun.bean.sys.PageBean;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProAffairException;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.exception.ProRunTimeException;
import com.fangcun.common.exception.ProValiDataException;
import com.fangcun.common.util.qiniu.QiniuUtils;
import com.fangcun.entity.Agent;
import com.fangcun.entity.AgentShareBenefitsRule;
import com.fangcun.entity.User;
import com.fangcun.mapper.AgentMapper;
import com.fangcun.mapper.AgentShareBenefitsRuleMapper;
import com.fangcun.mapper.UserMapper;
import com.fangcun.service.api.AgentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class AgentServiceImpl implements AgentService {

	@Autowired
	private AgentMapper agentMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private AgentShareBenefitsRuleMapper agentShareBenefitsRuleMapper;

	@Autowired
	private QiniuUtils qiniuUtils;

	@Override
	public void add(Agent request, Long userId) throws ProException {
		try {
			if (request.getId() == null) {
				if (agentMapper.insertSelective(request) == 0) {
					throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
				}
			}
			if (request.getId() != null) {
				update(request);
			}
		} catch (DataAccessException e) {
			throw new ProValiDataException(ErrCode.ERR_HAVE_EXISTED.getReturnCode(), ErrCode.ERR_HAVE_EXISTED.getReturnMsg());
		}
	}

	@Override
	public Agent details(Long userId, Long agentId) throws ProException {
		Agent agent = null;
		if (agentId != null) {
			agent = agentMapper.selectByPrimaryKey(agentId);
		} else {
			agent = agentMapper.selectByUserId(userId);
		}
		if (agent == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return agent;
	}

	@Override
	public User getPhone(String phone) throws ProException {
		User user = userMapper.selectByPhone(phone);
		if (user == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		Agent agent = agentMapper.selectByUserId(user.getId());
		if (agent != null) {
			throw new ProValiDataException(ErrCode.ERR_HAVE_EXISTED.getReturnCode(), ErrCode.ERR_HAVE_EXISTED.getReturnMsg());
		}
		return user;
	}

	@Override
	public void update(Agent request) throws ProException {
		if (!StringUtils.isEmpty(request.getCertificates())) {
			request.setCertificates(qiniuUtils.uploadImageUrl(request.getCertificates()));
		}
		if (!StringUtils.isEmpty(request.getBusinessLicense())) {
			request.setBusinessLicense(qiniuUtils.uploadImageUrl(request.getBusinessLicense()));
		}
		// 4 转为 1 等于接收邀请 4 转 5 拒绝
		if (agentMapper.updateByPrimaryKeySelective(request) <= 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public PageInfo<InvitationResponse> list(PageBean<Agent> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<InvitationResponse> list = agentMapper.selectByParentIds(request.getParam().getParentId());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	public InvitationRecordResponse invitation(Long userId) throws ProException {
		InvitationRecordResponse response = agentMapper.selectByRecord(userId);
		if (response == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return response;
	}

	@Override
	public PageInfo<Agent> AgentList(PageBean<Agent> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<Agent> list = agentMapper.selectByParentId(request.getParam().getParentId());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	@Transactional
	public AgentShareBenefitsRuleDetail detail(Long agentId) throws ProAffairException {
		AgentShareBenefitsRuleDetail detail = agentShareBenefitsRuleMapper.selectByAgentId(agentId);
		if (detail == null) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return detail;
	}

	@Override
	@Transactional
	public void settingShareBenefits(AgentShareBenefitsRule request) throws ProAffairException {
		AgentShareBenefitsRuleDetail detail = agentShareBenefitsRuleMapper.selectByAgentId(request.getAgentId());
		Agent agent = agentMapper.selectByPrimaryKey(request.getAgentId());
		if (agent == null) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		if (agent.getParentId() != null && agent.getParentId().longValue() != 0) {
			request.setParentAgentId(agent.getParentId());
		}
		if (request.getShareType() == 1) {
			if (request.getNumericalValue().doubleValue() > 1) {
				throw new ProRunTimeException(ErrCode.ERR_PARAMETER.getReturnCode(), ErrCode.ERR_PARAMETER.getReturnMsg());
			}
		}
		if (detail == null) {
			if (agentShareBenefitsRuleMapper.insertSelective(request) <= 0) {
				throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), "设置分利失败");
			}
		} else {
			request.setId(detail.getId());
			if (agentShareBenefitsRuleMapper.updateByPrimaryKeySelective(request) <= 0) {
				throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), "设置分利失败");
			}
		}
	}

}
